#include <stdio.h>
#include <stdbool.h>

#define QUIT_CHAR '#'

void p1() {
    int sum_enter = 0, sum_space = 0, sum_other = 0;
    char c;
    while ((c = getchar()) != QUIT_CHAR) {
        switch (c) {
            case '\x20':
                sum_space++;
                break;
            case '\n':
                sum_enter++;
                break;
            default:
                sum_other++;
                break;
        }
    }
    printf("'\\n'=%d,' '=%d,other=%d", sum_enter, sum_space, sum_other);
}

void p2() {
    int sum = 0;
    char c;
    while ((c = getchar()) != QUIT_CHAR) {
        if ('\n' == c) {
            sum = 8;
            continue;
        }
        if (sum % 8 == 0 && sum != 0) {
            putchar('\n');
        }
        sum++;
        printf("%c-%c ", c, c);
    }
}

void p3() {
    int sum_odd = 0;
    int sum_even = 0;
    int size_odd = 0;
    int size_even = 0;
    int d;
    printf("enter a integer, 0 to quit:");
    scanf("%d", &d);
    while (d != 0) {
        if (d % 2 == 1) {
            sum_odd += d;
            size_odd++;
        } else {
            sum_even += d;
            size_even++;
        }
        printf("enter next integer, 0 to quit:");
        scanf("%d", &d);
    }
    printf("%d size o odd and avg of odd is %.2f,%d size o even and avg of even is %.2f",
           size_odd, (float) sum_odd / size_odd, size_even, (float) sum_even / size_even);
}

void p4() {
    int sum_th = 0;
    int sum_jh = 0;
    char c;
    while (c = getchar()) {
        if (c == QUIT_CHAR) {
            break;
        } else if (c == '!') {
            putchar('!');
            putchar('!');
            sum_th++;
        } else if (c == '.') {
            sum_jh++;
            putchar('!');
        } else {
            putchar(c);
        }
    }
    printf("\n%d !,%d .,all %d\n", sum_th, sum_jh, sum_jh + sum_th);
}

void p5() {
    int sum_th = 0;
    int sum_jh = 0;
    char c;
    while (((c = getchar())) != QUIT_CHAR) {
        switch (c) {
            case QUIT_CHAR:
                goto r;
            case '!':
                putchar('!');
                putchar('!');
                sum_th++;
                break;
            case '.':
                sum_jh++;
                putchar('!');
                break;
            default:
                putchar(c);
                break;
        }
        r:;
    }
    printf("\n%d !,%d .,all %d\n", sum_th, sum_jh, sum_jh + sum_th);
}

void p6() {
    int sum_ei = 0;
    char c;
    char last_char = '\0';
    while (((c = getchar())) != QUIT_CHAR) {
        if ('e' == last_char && 'i' == c) {
            sum_ei++;
        }
        last_char = c;
    }
    printf("sum_ei = %d", sum_ei);
}

#define SALARY 10.0f
#define MUTLI_HOUR 1.5
#define MAX_HOUR 40
#define TAX_$300 .15f
#define TAX_ADD$150 .20f
#define TAX_OTHER .25f

void p7() {
    float hours;
    printf("Enter work hours of a week:");
    scanf("%f", &hours);
    float calculate_hours = hours > MAX_HOUR ? (hours - 40) * MUTLI_HOUR + 40 : hours;
    float brefore_tax_salary = calculate_hours * SALARY;
    float tax_number = brefore_tax_salary > 450 ?
                       300 * TAX_$300 + 150 * TAX_ADD$150 + (brefore_tax_salary - 450) * TAX_OTHER
                                                :
                       brefore_tax_salary > 300 ?
                       (brefore_tax_salary - 300) * TAX_ADD$150 + 300 * TAX_$300 : 300 * TAX_$300;
    float after_tax = brefore_tax_salary - tax_number;
    printf("总工资：%f,税金:%f,税后:%f", brefore_tax_salary, tax_number, after_tax);
}

void p8() {
    float hours;
    int select;
    float salary;
    do {
        printf("*************************************************************\n");
        printf("Enter the number corresponding to desired pay rate or aciton:\n");
        printf("1) $8.75/hr                     2）$9.33/hr\n");
        printf("3) $10.00/hr                    4）$11.20/hr\n");
        printf("5) quit\n");
        printf("*************************************************************\n");
        //TODO 这里怎么接收输入的值
        scanf("%d", &select);
        switch (select) {
            case 1:
                salary = 8.75f;
                break;
            case 2:
                salary = 9.33f;
                break;
            case 3:
                salary = 10.00f;
                break;
            case 4:
                salary = 11.20f;
                break;
            case 5:
                return;
            default:
                printf("only accept 1-5\n");
        }
    } while (select != 5);
    printf("Enter work hours of a week:");
    scanf("%f", &hours);
    float calculate_hours = hours > MAX_HOUR ? (hours - 40) * MUTLI_HOUR + 40 : hours;
    float brefore_tax_salary = calculate_hours * salary;
    float tax_number = brefore_tax_salary > 450 ?
                       300 * TAX_$300 + 150 * TAX_ADD$150 + (brefore_tax_salary - 450) * TAX_OTHER
                                                :
                       brefore_tax_salary > 300 ?
                       (brefore_tax_salary - 300) * TAX_ADD$150 + 300 * TAX_$300 : 300 * TAX_$300;
    float after_tax = brefore_tax_salary - tax_number;
    printf("总工资：%f,税金:%f,税后:%f", brefore_tax_salary, tax_number, after_tax);
}

_Bool isSuShu(int value) {
    for (int i = 2; i < value; ++i) {
        if (value % i == 0) {
            return false;
        }
    }
    return true;
}

void p9() {
    int i;
    printf("Enter a integer:");
    scanf("%d", &i);
    printf("小于或等于%d的素数有:");
    for (int j = 1; j <= i; ++j) {
        if (isSuShu(j)) {
            printf("%d,", j);
        }
    }
    printf("\b\n");
}

#define TAX_1 .15
#define TAX_2 .25

void p10() {
    double earning;
    int select;
    double min;
    do {
        printf("*************************************************************\n");
        printf("Enter the number corresponding to desired pay rate or aciton:\n");
        printf("1) 单身                     2）户主\n");
        printf("3) 已婚                     4）离异\n");
        printf("5) 退出\n");
        printf("*************************************************************\n");
        //TODO 这里怎么接收输入的值
        scanf("%d", &select);
        switch (select) {
            case 1:
                min = 17850.0;
                break;
            case 2:
                min = 23900.0;
                break;
            case 3:
                min = 29750.0;
                break;
            case 4:
                min = 14875.0;
                break;
            case 5:
                return;
            default:
                printf("only accept 1-5\n");
                goto b;
        }
        printf("Enter earning:");
        scanf("%lf", &earning);
        double tax = earning > min ? min * TAX_1 + (earning - min) * TAX_2 : earning * TAX_1;
        printf("税金:%f", tax);
        b:;
    } while (select != 5);
}

#define PRICE_FISH 2.05
#define PRICE_SUGARBEET 1.15
#define PRICE_CARROT 1.09
#define $100_PRIVILEGE .05

void p11() {
    double weight_fish = 0.0;
    double weight_sugarbeet = 0.0;
    double weight_carrot = 0.0;
    double privilege = 0.0;
    double freight_and_wrap = 0.0;
    int select;
    do {
        printf("*************************************************************\n");
        printf("请选择你要下单的物品:\n");
        printf("1) fish\n");
        printf("2) sugarbeet\n");
        printf("3) carrot\n");
        printf("4) order\n");
        printf("5) exit\n");
        printf("*************************************************************\n");
        //TODO 这里怎么接收输入的值
        scanf("%d", &select);
        double number;
        switch (select) {
            case 1:
                printf("输入你要买的鱼重量:\n");
                scanf("%lf", &number);
                weight_fish += number;
                printf("当前购买信息: 鱼-%.2f磅,甜菜-%.2f磅,胡萝卜-%.2f磅\n", weight_fish, weight_sugarbeet, weight_carrot);
                break;
            case 2:
                printf("输入你要买的甜菜重量:\n");
                scanf("%lf", &number);
                weight_sugarbeet += number;
                printf("当前购买信息: 鱼-%.2f磅,甜菜-%.2f磅,胡萝卜-%.2f磅\n", weight_fish, weight_sugarbeet, weight_carrot);
                break;
            case 3:
                printf("输入你要买的胡萝卜重量:\n");
                scanf("%lf", &number);
                weight_carrot += number;
                printf("当前购买信息: 鱼-%.2f磅,甜菜-%.2f磅,胡萝卜-%.2f磅\n", weight_fish, weight_sugarbeet, weight_carrot);
                break;
            case 4:
                printf("恭喜你下单完成，订单信息如下:\n\n");
                printf("******************************************\n");
                printf("**%10s%13s%13s**\n", "type", "weight","cost");
                double weight = weight_carrot + weight_fish + weight_sugarbeet;
                double total_price =
                        weight_carrot * PRICE_CARROT + weight_fish * PRICE_FISH +
                        weight_sugarbeet * PRICE_SUGARBEET;
                if (weight_fish > 0) {
                    printf("**%10s%8.2fpound%13.2f$ **\n", "fish", weight_fish,weight_fish * PRICE_FISH);
                }
                if (weight_sugarbeet > 0) {
                    printf("**%10s%8.2fpound%13.2f$ **\n", "sugarbeet", weight_sugarbeet,weight_sugarbeet * PRICE_SUGARBEET);
                }
                if (weight_carrot > 0) {
                    printf("**%10s%8.2fpound%13.2f$ **\n", "carrot", weight_carrot,weight_carrot * PRICE_CARROT);
                }
                printf("**%10s%8.2fpound%13.2f$ **\n", "sum", weight,total_price);
                double f1 = 6.5;
                double f2 = 14;

                freight_and_wrap = weight > 20 ? (weight - 20) * .5 + f2 : weight > 5 ? 14 : f1;
                privilege = total_price >100? total_price * $100_PRIVILEGE : 0.0;
                printf("运费和包装费：%.2f\n", freight_and_wrap);
                printf("优惠：%.2f\n", privilege);
                printf("应付：%.2f\n",total_price-privilege);
                printf("******************************************\n");
                //todo 订单信息
                break;
            case 5:
                return;
            default:
                printf("only accept 1-5!\n");
                goto b;
        }
        b:;
    } while (select != 5 && select!=4);
}

int main(void) {

//    p1();
//    p2();
//    p3();
//    p4();
//    p5();
//    p6();
//    p7();
//    p8();
//    p9();
//    p10();
    p11();
    return 1;
}